package com.ball.web;

import com.alibaba.fastjson.JSONObject;
import com.ball.entity.User;
import com.ball.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ball.tools.StringUtil.isNull;


@Controller
@RequestMapping("/game")
public class GameController extends AbstractController {

    @Autowired
    private WalletService walletService;
    @Autowired
    private LogService logService;
    @Autowired
    private UserService userService;
//    @RequestMapping(value = "/listGame")
//    public String index(Model model,@RequestBody Map<String, Object> map) {
//        Map<String,Object> result =new HashMap<String,Object>();
//        JSONObject json=new JSONObject(map);
//        System.out.println(json.toJSONString());
//        System.out.println(gameService.queryGameList(json));
//        model.addAttribute("baseURL", getContextPath());
//        model.addAttribute("games", gameService.queryGameList(json));
//        return "game";
//    }
//    @RequestMapping(value = "")
//    @ResponseBody
//    public String queryGameList(Model model){
//        model.addAttribute("baseURL",getContextPath());
//        return "game";
//    }
//    @RequestMapping("")
//    public String listGame(Model m,@RequestParam(value = "start", defaultValue = "0") int start,@RequestParam(value = "size", defaultValue = "5") int size) throws Exception {
//        PageHelper.startPage(start,size,"id desc");
//        List<Game> games=gameService.queryGameList(new JSONObject());
//        PageInfo<Game> page = new PageInfo<>(games);
//        m.addAttribute("page", page);
//        return "game";
//    }
    @RequestMapping("")
    public String Game() {
        return "game";
    }

//    @RequestMapping(value = "/bet", method = RequestMethod.POST, produces = "application/json")
//    @ResponseBody
//    public Map<String, Object> bet(@RequestBody Map<String, Object> map) {
//        Map<String, Object> result = new HashMap<String, Object>();
//        JSONObject json = new JSONObject(map);
//        String gameId = json.getString("gameId");
//        String username = json.getString("username");
//        String pay = json.getString("pay");
//        String team = json.getString("team");
//        json.put("id", gameId);
//        String address = gameService.queryGameById(json).getWalletAddress();
//        System.out.println(gameId + username + pay + team + address);
//        if (Double.valueOf(pay) > 0.001 && !address.isEmpty()) {
//            json.put("address", address);
//            List<String> list = walletService.transfer(json);
//            int i = 0;
//            do {
//                try {
//
//                    Thread.sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                i++;
//            } while (isNull(list.get(1)) && i < 10);
//            System.out.println(list);
//            String b = userOfGameService.add(json);
//            System.out.println(b);
//            json.put("log", list.get(1));
//            String a = logService.add(json);
//            System.out.println(a);
//            result.put("result", "1");
//        }
//        result.put("result", "2");
////        if(!(username.equals("")||password.equals(""))){
////            System.out.println(username+password);
////            User user=userService.queryUserByUsername(json);
////            if(StringUtils.isEmpty(user)){
////                //账号未注册
////                result.put("result","3");
////            }
////            else{
////                if(user.getPassword().equals(password)){
////                    //登入成功
////                    request.getSession().setAttribute("user",user);
////                    result.put("result","1");
////                }else{
////                    //密码错误
////                    result.put("result","2");
////                }
////
////            }
////        }else{
////            System.out.println("111");
////            result.put("result","0");
////        }
////        System.out.println(result.toString());
//        return result;
//    }

    @RequestMapping(value = "/transfer", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> transfer(@RequestBody Map<String, Object> map) throws InterruptedException {
        Map<String, Object> result = new HashMap<String, Object>();
        JSONObject json = new JSONObject(map);
        String odds = json.getString("odds");
        String username = json.getString("username");
        String pay = json.getString("pay");
        logger.info(odds+username+pay);
        Double payy=Double.valueOf(pay);
        User user=userService.queryUserByUsername(json);
        String address=user.getWalletAddress();
        logger.info(username + pay + address);
        double b=Math.random();
        List<String> list = walletService.balance(json);
        int i = 0;
        do {
            Thread.sleep(1000);
            i++;
        } while (isNull(list.get(1)) != true && i < 10);
        String money = list.get(1);
        logger.info(money);
        json.put("log", money);
        logService.add(json);
        money = money.substring(money.indexOf("balance") + 9, money.indexOf(","));
        user.setMoney(money);
        userService.update(user);
        double moneyy=Double.valueOf(money);
        logger.info(String.valueOf(moneyy));
        logger.info(String.valueOf(payy));
        if(!((moneyy-payy)>0.001)) {
            result.put("result", "3");
            return result;
        }
        logger.info(String.valueOf(b));
        //b>0.5赢了
        if (b>=0.5) {
            if (odds.equals("big")) {
                json.put("username","rootroot");
                json.put("address",address);
                payy=payy*2.0;
                json.put("pay",payy.toString());
                list = walletService.transfer(json);
                i = 0;
                do {
                    try {

                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                } while (isNull(list.get(1)) && i < 10);
                logger.debug(list.get(1));
                json.put("log", list.get(1));
                logService.add(json);
                result.put("result", "1");
            } else if (odds.equals("small")) {
                json.put("address","fyTaiwNhWEuRA8NLxddFBEgq71xRqVZsHauAvmdjyxfX8J1KwLqLoNFEmnBBbY2owkF836VpXs4kRGYmcLeKNL421EWkRTVCb");
                json.put("pay",payy.toString());
                list = walletService.transfer(json);
                do{

                }while (isNull(list.get(1)));
//                i = 0;
//                do {
//                    try {
//
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    i++;
//                } while (isNull(list.get(1)) && i < 10);
                logger.info(list.get(1));
                json.put("log", list.get(1));
                logService.add(json);
                result.put("result", "2");
            }
            else {

            }
        }
        else {
            if (odds.equals("big")) {
                json.put("address","fyTaiwNhWEuRA8NLxddFBEgq71xRqVZsHauAvmdjyxfX8J1KwLqLoNFEmnBBbY2owkF836VpXs4kRGYmcLeKNL421EWkRTVCb");
                json.put("pay",payy.toString());
                list = walletService.transfer(json);
                i = 0;
                do {
                    try {

                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                } while (isNull(list.get(1)) && i < 10);
                logger.info(list.get(1));
                json.put("log", list.get(1));
                logService.add(json);
                result.put("result", "2");
            } else if (odds.equals("small")) {
                json.put("username","rootroot");
                json.put("address",address);
                payy=payy*2.0;
                json.put("pay",payy.toString());
                list = walletService.transfer(json);
                i = 0;
                do {
                    try {

                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    i++;
                } while (isNull(list.get(1)) && i < 10);
                logger.info(list.get(1));
                json.put("log", list.get(1));
                logService.add(json);
                result.put("result", "1");
            }
            else {

            }
        }
        if((list.get(1).indexOf("ERROR"))!=-1){
            result.put("result","3");
        }
        logger.debug(result.toString());
        return result;
    }
}
